﻿// Copyright (c) 2010 Dan Popick

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;

using Open.Web.Stijl.Css;
using Open.Web.Stijl.Utility;

namespace Open.Web.Stijl
{
    public class SpriteContainer: IEnumerable<Sprite>
    {
        public PixelFormat Format { get; set; }
		public RepeatType Repeat { get; set; }
        public List<Sprite> Sprites { get; private set; }

        public ColorHistogram ColorHistogram { get; private set; }
        public ColorSet Colors { get; private set; }
        public string Name { get; private set; }

        public SpriteContainer(PixelFormat format, RepeatType repeat)
        {
            Format = format;
			Repeat = repeat;
            Sprites = new List<Sprite>();
            Colors = new ColorSet();
        }

        public SpriteContainer(IEnumerable<Sprite> sprites)
            :this(sprites.First().Format, sprites.First().Repeat)
        {
            foreach (var sprite in sprites)
                Add(sprite);
        }

        public void Add(Sprite sprite)
        {
            if (sprite.Format != Format)
                throw new ArgumentException(String.Format("Format does not match: {0}, {1}", sprite.Format, Format, "sprite"));

            ColorHistogram = sprite.ColorHistogram | ColorHistogram;
            Colors.UnionWith(sprite.Colors);
            Sprites.Add(sprite);
        }

        public void Add(SpriteContainer sprites)
        {
            if (sprites.Format != Format)
                throw new ArgumentException(String.Format("Format does not match: {0}, {1}", sprites.Format, Format, "sprite"));

            ColorHistogram = sprites.ColorHistogram | ColorHistogram;
            Colors.UnionWith(sprites.Colors);
            Sprites.AddRange(sprites.Sprites);
        }

        public IEnumerator<Sprite> GetEnumerator()
        {
            return Sprites.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Sprites.GetEnumerator();
        }
    }
}
